package com.qingcheng.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.qingcheng.dao.StockBackMapper;
import com.qingcheng.pojo.goods.StockBack;
import com.qingcheng.pojo.order.OrderItem;
import com.qingcheng.service.goods.StockBackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * @Title: StockBackServiceImpl
 * @Description:
 * @Auther:
 * @Version: 1.0
 * @create 2021/10/15 14:28
 */
@Service(interfaceClass = StockBackService.class)
public class StockBackServiceImpl implements StockBackService {

    @Autowired
    private StockBackMapper stockBackMapper;
    @Transactional
    public void insert(List<OrderItem> orderItems) {
        for (OrderItem orderItem : orderItems){
            StockBack stockBack = new StockBack();
            stockBack.setOrderId(orderItem.getOrderId());
            stockBack.setSkuId(orderItem.getSkuId());
            stockBack.setNum(orderItem.getNum()); // 要回滚的数量
            stockBack.setStatus("0");//未回滚
            stockBack.setCreateTime(new Date());
            stockBackMapper.insert(stockBack);


        }
    }

    public List<StockBack> queryAll() {
        Example example = new Example(StockBack.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status","0");


        List<StockBack> stockBacks = stockBackMapper.selectByExample(example);

        return stockBacks;
    }

    @Transactional
    public void updateState(List<StockBack> stockBacks) {
        for (StockBack stockBack : stockBacks){
            stockBackMapper.updateStatus(stockBack.getOrderId(),stockBack.getSkuId());
        }
    }
}
